// 子模块moduleA路径：store/modules/moduleA.js 
export default {
	state: uni.getStorageSync('UserState'),
	getters: {},
	mutations: {
		addUser(state, res) {
			console.log('addUser', res);
			console.log('addUser', uni.getStorageSync('UserState'));
			if (state.userinfo) {
				// 不是第一次进入
				console.log('888888888888888')
				// for (let i in state.userinfo) {}
				let exictIndex = state.userinfo.findIndex(stateItem => {
					return res.data.mobile == stateItem.data.mobile;
				});
				if (exictIndex === -1) {
					// for (let i in state.userinfo) {
					// 	state.userinfo[i].iscurrentSelect = false;
					// }
					state.userinfo.push({
						token: res.token,
						data: res.data,
					});

					state.iscurrentSelect = state.userinfo.length - 1;
					uni.setStorageSync('UserState', state);
					console.log('store------------', uni.getStorageSync('UserState'));
				} else {
					state.iscurrentSelect = exictIndex;
					uni.setStorageSync('UserState', state);
					console.log('store------------', uni.getStorageSync('UserState'));
				}
			} else {
				console.log('9999999999999')
				state.userinfo = [];
				// 第一个用户进来了
				state.userinfo.push({
					token: res.token,
					data: res.data,
				});
				console.log('11111111111111')
				// state.userinfo[0].iscurrentSelect = false;
				state.iscurrentSelect = 0;
				uni.setStorageSync('UserState', state);
				console.log('store------------', uni.getStorageSync('UserState'));
			}



		},
		choseUser(state, currentChose) {
			state.iscurrentSelect = currentChose;
			uni.setStorageSync('AUTH_TOKEN', state.userinfo[currentChose].token);
		},
		removeUser(state, currentChose) {

			console.log('移除指定账号')
			state.userinfo.splice(currentChose, 1);
			if (state.userinfo.length === 0) {
				uni.setStorageSync('AUTH_TOKEN', '');
			} else {
				uni.setStorageSync('AUTH_TOKEN', state.userinfo[currentChose].token);
			}
			uni.setStorageSync('UserState', state);
		},
		removeAllUser(state, topageurl) {

			state.userinfo = [];
			try {
				uni.clearStorageSync();
				console.warn('清理成功');
				uni.showToast({
					icon: 'none',
					title: '退出成功'
				});
				uni.setStorageSync('UserState', '');
				console.warn(topageurl);
				setTimeout(() => {
					if (topageurl === 'login') {
						uni.redirectTo({
							url: '/pages/login'
						});
					} else if (topageurl === 'aReward') {
						uni.switchTab({
							url: '/pages/aReward/aReward'
						});
					}
				}, 1000);

			} catch (e) {
				console.warn('清理失败');
				// error
				uni.showToast({
					icon: 'none',
					title: '退出失败'
				});
			}
		},
		updateUserInfo(state, updateDict) {

			// console.log('-------进入updateUserInfo方法', state);
			// console.log('-------进入updateUserInfo方法', updateDict);
			// console.log('-------进入updateUserInfo方法', state.iscurrentSelect);
			if (updateDict.key === 'head_image') {
				state.userinfo[state.iscurrentSelect].data.head_image = updateDict.value;
			} else if (updateDict.key === 'user_name') {
				state.userinfo[state.iscurrentSelect].data.user_name = updateDict.value;
			} else {
				console.log('走到这里就是有问题');
				// state.userinfo[state.iscurrentSelect].data.mobile = updateDict.value;
			}
			console.log('修改完------', state);
			uni.setStorageSync('UserState', state);
		},
		changeSwitchState(state, bool) {
			state.isSwitch = bool;
		}
	},
	actions: {

	}
}
